$(function() {
	
	var sURL = unescape(window.location.pathname);
	setTimeout(function() {
		window.location.href = sURL;
	}, 600000);
	$('.tbl-holder table tbody tr:last td').css('borderBottom', 'none');
	
	var ajaxLoader = '<img id="ajaxLoader" src="/i/ajax-loader-small.gif" width="16" height="16" alt="" />';
	
	$('.tbl-holder .tbl-holder-top .action .hide').bind('click', hideOrdersBlock);
	$('.tbl-holder .tbl-holder-top .action .show').bind('click', showOrdersBlock);
	$('.tbl-holder .more-items a.more').bind('click', getMoreOrders);
	
	function hideOrdersBlock(e) {
		var $this = $(this);
		var container = $this.parents('.tbl-holder');
		var action = 'hide';
		var expState = container.find('input[type="hidden"].exp-state').val();
		$('.tbl-holder .tbl-holder-top .action .hide').unbind('click', hideOrdersBlock);
		$this.before(ajaxLoader);
		$.ajax({
			cache	: false,
			async   : true,
			type    : "POST",
			url     : "/a_admin/order.changeOrderListParams",
			dataType: "json",
			data    : {expState: expState, action: action},
			success : function(data){
				if (data.status == 'error') {
					alert('ERROR_ORDER_RESPONSE_TEXT'.translate());
				}
				$this.hide();
				container.find('table').hide();
				container.find('.more-items').hide();
				container.find('.tbl-holder-top .action .show').show();
				$('#ajaxLoader').remove();
				$('.tbl-holder .tbl-holder-top .action .hide').bind('click', hideOrdersBlock);
				container.find('.tbl-holder-top span span').text(0);
			},
			error : function() {
				$this.hide();
				container.find('table').hide();
				container.find('.more-items').hide();
				container.find('.tbl-holder-top .action .show').show();
				$('#ajaxLoader').remove();
				$('.tbl-holder .tbl-holder-top .action .hide').bind('click', hideOrdersBlock);
				alert('Error with requesting ajax data.');
			}
		});
	}

	function showOrdersBlock() {
		var $this = $(this);
		var container = $this.parents('.tbl-holder');
		var action = 'open';
		var expState = container.find('input[type="hidden"].exp-state').val();
		$('.tbl-holder .tbl-holder-top .action .show').unbind('click', showOrdersBlock);
		$this.before(ajaxLoader);
		$.ajax({
			cache	: false,
			async   : true,
			type    : "POST",
			url     : "/a_admin/order.changeOrderListParams",
			dataType: "json",
			data    : {expState: expState, action: action},
			success : function(data){
				if (data.status == 'error') {
					alert('ERROR_ORDER_RESPONSE_TEXT'.translate());
				}
				$this.hide();
				container.find('.more-items').show();
				container.find('.tbl-holder-top .action .hide').show();
				container.find('table').show();
				$('#ajaxLoader').remove();
				container.find('table tbody').html(data.html);
				$('.tbl-holder .tbl-holder-top .action .show').bind('click', showOrdersBlock);
				var ordersCount = 0;
				ordersCount = container.find('tr').size() - 1;
				if(ordersCount == parseInt(container.find('.tbl-holder-top span b').text())) {
					container.find('.more-items').hide();
				}
				container.find('.tbl-holder-top span span').text(ordersCount);
			},
			error : function() {
				$this.hide();
				container.find('.more-items').show();
				container.find('.tbl-holder-top .action .hide').show();
				container.find('table').show();
				$('#ajaxLoader').remove();
				$('.tbl-holder .tbl-holder-top .action .show').bind('click', showOrdersBlock);
				alert('Error with requesting ajax data.');
			}
		});
	}

	function getMoreOrders() {
		var action = 'more';
		var $this = $(this);
		var container = $this.parents('.tbl-holder');
		var expState = container.find('input[type="hidden"].exp-state').val();
		$this.before(ajaxLoader);
		$.ajax({
			cache	: false,
			async   : true,
			type    : "POST",
			url     : "/a_admin/order.changeOrderListParams",
			dataType: "json",
			data    : {expState: expState, action: action},
			success : function(data){
				if (data.status == 'success' && data.html != '') {
					container.find('table tbody').append(data.html);
				} else if (data.status == 'error') {
					alert('ERROR_ORDER_RESPONSE_TEXT'.translate());
				}
				var ordersCount = 0;
				ordersCount = container.find('tr').size() - 1;
				if(ordersCount == parseInt(container.find('.tbl-holder-top span b').text())) {
					container.find('.more-items').hide();
				}
				container.find('.tbl-holder-top span span').text(ordersCount);
				$('#ajaxLoader').remove();
			},
			error : function() {
				$('#ajaxLoader').remove();
				alert('Error with requesting ajax data.');
			}
		});
	}
});